package pl.edu.pk.mech.kkbus.controller;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pl.edu.pk.mech.kkbus.dto.Award;
import pl.edu.pk.mech.kkbus.model.ResponseStatus;
import pl.edu.pk.mech.kkbus.service.AwardService;

@Controller
public class AwardController extends RestController {

    @Autowired
    AwardService awardService;
	
    private static final Logger log = Logger.getLogger(AwardController.class.getName());
    
    @RequestMapping(value = "/awards", method = RequestMethod.GET)
    public @ResponseBody List<Award> showAwards() {
        return awardService.getAllAwards();         
    }
    
    @RequestMapping(value = "/award", method = RequestMethod.POST)
    public @ResponseBody ResponseStatus create(@RequestBody Award award) {
        log.info("Create new award.");
    	boolean result = awardService.create(award);
        return result ? ResponseStatus.ADDED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/award/{id}", method = RequestMethod.PUT)
    public @ResponseBody ResponseStatus update(@PathVariable long id, @RequestBody Award award) {
        log.info("Update award with id " + id);
        boolean result = awardService.update(id, award);
        return result ? ResponseStatus.UPDATED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/award/{id}", method = RequestMethod.DELETE)
    public @ResponseBody ResponseStatus delete(@PathVariable long id) {
        boolean result = awardService.delete(id);
        return result ? ResponseStatus.DELETED : ResponseStatus.ERROR;
    }
   
}
